package com.appspot.secretcuz.service;

import java.util.List;

import com.appspot.secretcuz.entity.BlogPost;
import com.google.appengine.api.datastore.Cursor;
import com.googlecode.objectify.cmd.Query;

import static com.appspot.secretcuz.entity.OfyService.ofy;

public class BlogPostService {
	
	public static List<BlogPost> get() {
		return ofy().load().type(BlogPost.class).order("-date").list();
	}
	
	public static BlogPost get(Long id) {
		BlogPost bp = ofy().load().type(BlogPost.class).id(id).get();
		return bp;
	}
	
	public static Query<BlogPost> get(Cursor cursor, int limit) {
		if (cursor == null) {
			return ofy().load().type(BlogPost.class).limit(limit);
		}
		return ofy().load().type(BlogPost.class).limit(limit).startAt(cursor);
	}

	public static BlogPost save(BlogPost bp) {
		return get(ofy().save().entity(bp).now().getId());
	}
	
	public static void delete(BlogPost bp) {
		ofy().delete().entity(bp);
	}
}
